/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.DepartmentDAO;
import dao.EmployeeDAO;
import dao.EmployeeDiaryDAO;
import dao.ProjectDAO;
import dto.Department;
import dto.Employee;
import dto.EmployeeDiary;
import dto.Location;
import dto.Project;
import java.awt.Component;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import render.CTCComboboxModel;
import utils.ComponentsUtil;
import utils.FPanel;

/**
 *
 * @author Canh
 */
public class EmployeeDiaryCreateForm extends FPanel {

    /**
     * Creates new form EmployeeDiaryCreateForm
     */
    Employee employee;
    EmployeeDiaryDAO dao;

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
        loadView();
        reloadData();
    }

    public EmployeeDiaryCreateForm(boolean isEditable) {
        initComponents();
        if (MainFrame.employee != null) {
            this.setEmployee(MainFrame.employee);
        }

        this.setHiddenList(new Component[]{createDateField, idField, employeeNumberField, currentDepartmentField, currentProjectField});
        setEditValue(isEditable);
    }

    void loadView() {
        ProjectDAO pDao = new ProjectDAO();
        DepartmentDAO dDao = new DepartmentDAO();
        transferProjectField.setModel(new CTCComboboxModel(pDao.readAll(), "getName"));
        transferDepartmentField.setModel(new CTCComboboxModel(dDao.readAll(), "getName"));
        currentProjectField.setText(pDao.readByID(employee.getProjectId()).getName());
        currentDepartmentField.setText(dDao.readByID(employee.getDepartmentId()).getName());
        employeeNumberField.setText(String.valueOf(employee.getEmpNum()));
    }

    void reloadData() {
        dao = new EmployeeDiaryDAO();
        ArrayList<EmployeeDiary> diaries = dao.readByEmployee(employee);
        EmployeeDiary d = dao.lastTransfer(employee);
        if (d != null && d.getStatus() == EmployeeDiary.TRANSFER_STATUS.TRANSFER_UNVIEW) {
            transferDepartmentField.setSelectedIndex(
                    ComponentsUtil.findIndex(((CTCComboboxModel) transferDepartmentField.getModel()).getData(), employee.getDepartmentId(), "getId")
            );
            transferProjectField.setSelectedIndex(
                    ComponentsUtil.findIndex(((CTCComboboxModel) transferProjectField.getModel()).getData(), employee.getProjectId(), "getId")
            );
            createDateField.setDate(d.getCreateDate());
            idField.setText(String.valueOf(d.getId()));
            reasonField.setText(d.getReason());
        } else {
            createDateField.setDate(new Date());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        reasonLabel = new javax.swing.JLabel();
        idField = new javax.swing.JTextField();
        deleteButton = new javax.swing.JButton();
        employeeNumberField = new javax.swing.JTextField();
        currentProjectField = new javax.swing.JTextField();
        currentDepartmentField = new javax.swing.JTextField();
        idLabel = new javax.swing.JLabel();
        employeeNumberLabel = new javax.swing.JLabel();
        currentProjectLabel = new javax.swing.JLabel();
        currentDepartmentLabel = new javax.swing.JLabel();
        transferProjectLabel = new javax.swing.JLabel();
        transferDepartmentLabel = new javax.swing.JLabel();
        saveButton = new javax.swing.JButton();
        createDateLabel = new javax.swing.JLabel();
        refreshButton = new javax.swing.JButton();
        transferProjectField = new javax.swing.JComboBox();
        transferDepartmentField = new javax.swing.JComboBox();
        reasonFieldScroll = new javax.swing.JScrollPane();
        reasonField = new javax.swing.JTextArea();
        createDateField = new com.toedter.calendar.JDateChooser();

        reasonLabel.setText("Reason:");

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        idLabel.setText("Id:");

        employeeNumberLabel.setText("Employee Number:");

        currentProjectLabel.setText("Current Project:");

        currentDepartmentLabel.setText("Current Department:");

        transferProjectLabel.setText("Transfer Project:");

        transferDepartmentLabel.setText("Transfer Department:");

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/save.png"))); // NOI18N
        saveButton.setText("Save");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        createDateLabel.setText("Create Date:");

        refreshButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/reload.png"))); // NOI18N
        refreshButton.setText("Refresh");
        refreshButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshButtonActionPerformed(evt);
            }
        });

        transferProjectField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        transferDepartmentField.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        reasonField.setColumns(20);
        reasonField.setRows(5);
        reasonFieldScroll.setViewportView(reasonField);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(refreshButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(idLabel)
                            .addComponent(employeeNumberLabel)
                            .addComponent(currentProjectLabel)
                            .addComponent(currentDepartmentLabel)
                            .addComponent(transferProjectLabel)
                            .addComponent(transferDepartmentLabel)
                            .addComponent(createDateLabel)
                            .addComponent(reasonLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(idField, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                            .addComponent(employeeNumberField, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                            .addComponent(currentProjectField, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                            .addComponent(currentDepartmentField, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                            .addComponent(transferProjectField, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(transferDepartmentField, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(reasonFieldScroll)
                            .addComponent(createDateField, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idLabel)
                    .addComponent(idField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(employeeNumberLabel)
                    .addComponent(employeeNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(currentProjectLabel)
                    .addComponent(currentProjectField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(currentDepartmentLabel)
                    .addComponent(currentDepartmentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(transferProjectLabel)
                    .addComponent(transferProjectField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(transferDepartmentLabel)
                    .addComponent(transferDepartmentField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(createDateLabel)
                    .addComponent(createDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(reasonLabel)
                        .addGap(0, 178, Short.MAX_VALUE))
                    .addComponent(reasonFieldScroll))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(refreshButton)
                    .addComponent(deleteButton))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        try {
            dao.delete(Integer.parseInt(idField.getText()));
        } catch (Exception ex) {
        }
        loadView();
        reloadData();
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        try {
            if (readItem() == null) {
                JOptionPane.showMessageDialog(this, "Enter Wrong Information!");
            } else {
                if (readItem().getId() == -1) {
                    dao.create(readItem());
                } else {
                    dao.update(readItem());
                }
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Enter Wrong Information!");
        }
        loadView();
        reloadData();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void refreshButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshButtonActionPerformed
        refresh();
    }//GEN-LAST:event_refreshButtonActionPerformed
    public static void main(String[] args) {

        JFrame fr = new JFrame();
        EmployeeDiaryCreateForm crForm = new EmployeeDiaryCreateForm(true);
        crForm.setEmployee(new EmployeeDAO().readById(2));
        fr.getContentPane().add(crForm);
        fr.pack();
        fr.setVisible(true);
    }

    EmployeeDiary readItem() {
        EmployeeDiary d = new EmployeeDiary();
        if (idField.getText().equals("")) {
            d = new EmployeeDiary();
            d.setId(-1);
            d.setEmployeeNumber(employee.getEmpNum());
            d.setCreateDate(createDateField.getDate());
            d.setCurrentDepartment(employee.getDepartmentId());
            d.setCurrentProject(employee.getProjectId());
            d.setReason(reasonField.getText());
            d.setTransferProject(
                    ((Project) ((CTCComboboxModel) transferProjectField.getModel()).getData().get(transferProjectField.getSelectedIndex())).getId()
            );
            d.setTransferDepartment(
                    ((Department) ((CTCComboboxModel) transferDepartmentField.getModel()).getData().get(transferDepartmentField.getSelectedIndex())).getId()
            );

        } else {
            d = new EmployeeDiary();
            d.setId((Integer.parseInt(idField.getText())));
            d.setEmployeeNumber(employee.getEmpNum());
            d.setCreateDate(createDateField.getDate());
            d.setCurrentDepartment(employee.getDepartmentId());
            d.setCurrentProject(employee.getProjectId());
            d.setReason(reasonField.getText());
            d.setTransferProject(
                    ((Project) ((CTCComboboxModel) transferProjectField.getModel()).getData().get(transferProjectField.getSelectedIndex())).getId()
            );
            d.setTransferDepartment(
                    ((Department) ((CTCComboboxModel) transferDepartmentField.getModel()).getData().get(transferDepartmentField.getSelectedIndex())).getId()
            );

        }
        return d;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser createDateField;
    private javax.swing.JLabel createDateLabel;
    private javax.swing.JTextField currentDepartmentField;
    private javax.swing.JLabel currentDepartmentLabel;
    private javax.swing.JTextField currentProjectField;
    private javax.swing.JLabel currentProjectLabel;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTextField employeeNumberField;
    private javax.swing.JLabel employeeNumberLabel;
    private javax.swing.JTextField idField;
    private javax.swing.JLabel idLabel;
    private javax.swing.JTextArea reasonField;
    private javax.swing.JScrollPane reasonFieldScroll;
    private javax.swing.JLabel reasonLabel;
    private javax.swing.JButton refreshButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JComboBox transferDepartmentField;
    private javax.swing.JLabel transferDepartmentLabel;
    private javax.swing.JComboBox transferProjectField;
    private javax.swing.JLabel transferProjectLabel;
    // End of variables declaration//GEN-END:variables
}
